//
//  BaseTableVC.swift
//  TravelSwift
//
//  Created by  on 2018/12/24.
//  Copyright © 2018 蜀归科技. All rights reserved.
//

import Foundation
import UIKit
import EmptyDataSet_Swift

open class ZMTableViewController: UITableViewController {
    
    let disposeBag = DisposeBag()
    override open func viewDidLoad() {
        if (self.navigationController?.viewControllers.count)! > 1 {
            #warning("修改返回按钮图片")
            setLeftBtn(imgStr: "back_black")
        }
//        tableView = UITableView(frame: .zero, style: .grouped)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        hbd_barTintColor = .white
        hbd_barShadowHidden = true
        hbd_titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.zm.color333333(), NSAttributedString.Key.font: UIFont.zm.fontSemibold(size: 16)]
        view.backgroundColor = .white
    }
    
//    open override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        if #available(iOS 11.0, *) {
//            self.edgesForExtendedLayout = UIRectEdge()
//        }else{
//            self.automaticallyAdjustsScrollViewInsets = true
//        }
//    }
    
    /// 设置返回按钮
    func setLeftBtn(imgStr: String) -> Void {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        btn.setImage(UIImage.init(named: imgStr), for: .normal)
//        btn.setImage(Base_Bundle_Img(imgName: imgStr), for: .normal)
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: btn)
        btn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
    }
    
    /// 添加右侧图片按钮
    func setRightImgBtn(imgStr: String) -> Void {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        btn.setImage(UIImage.init(named: imgStr), for: .normal)
        btn.addTarget(self, action: #selector(rightBtnClick), for: .touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: btn)
    }
    
    /// 添加右侧自定义按钮
    func setRightCustomBtn(titleStr: String, color: UIColor, font: UIFont) -> Void {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        btn.setTitle(titleStr, for: .normal)
        btn.titleLabel?.font = font
        btn.setTitleColor(color, for: .normal)
        btn.addTarget(self, action: #selector(rightBtnClick), for: .touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: btn)
    }

    
    // MARK: -----------------  空数据属性，添加  -----------------
    /// 空数据提示文字
    public var emptyTitle = ""
    /// 空数据提示文字字体
    public var emptyTitleFont = UIFont.zm.fontRegular(size: 14)
    /// 空数据提示文字颜色
    public var emptyTitleColor = UIColor.zm.color999999()
    /// 空数据提示图片
    public var emptyImg = ""
    /// 空数据提示偏移
    public var emptyOffset = 0
    /// 空数据按钮图片，按钮点击方法子类实现
    public var emptyButtonImage = ""
    /// 空数据背景色，默认UIScrollView背景色
    public var emptyBgColor: UIColor?
    /// 空数据是否可以滑动，默认true
    public var emptyScroll = true
    func addNodataTalbelView() -> Void {
        self.tableView.emptyDataSetSource   = self
        self.tableView.emptyDataSetDelegate = self
    }
    
    // MARK: -----------------  MJRefresh  -----------------
    /// 上拉加载，下拉刷新
    /// - Parameter scrollView: scrollView
    public func addRefresh(scrollView: UIScrollView, addFoot: Bool = true, complete: ((_ refresh : Bool) ->Void)?) {
        scrollView.mj_header = ZMRefreshView.addNormalRefreshHeaderWithBlock(complete: {
            complete!(true)
        })
        if addFoot {
            scrollView.mj_footer = ZMRefreshView.addBackNormalFooterWithBlock(complete: {
                complete!(false)
            })
        }
    }
    
    deinit {
        ZMLog(message: "------------------ \(self.classForCoder) 释放")
    }
}

// MARK: -----------------  导航返回，右侧按钮事件  -----------------
extension ZMTableViewController {
    /// 返回事件
    @objc func leftBtnClick(sender: UIButton) -> Void {
        navigationController?.popViewController(animated: true)
    }
    
    /// 右侧按钮事件，子类实现
    @objc func rightBtnClick(sender: UIButton) -> Void {}
}


extension ZMTableViewController: EmptyDataSetSource, EmptyDataSetDelegate{
    /// 空数据标题
    public func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let attributes = [NSAttributedString.Key.foregroundColor: emptyTitleColor, NSAttributedString.Key.font: emptyTitleFont]
        return NSAttributedString(string: emptyTitle, attributes: attributes)
    }
    
    /// 空数据图片
    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return UIImage(named: emptyImg)
    }
    
    /// 空数据按钮图片
    public func buttonImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> UIImage? {
        return UIImage(named: emptyButtonImage)
    }
    
    /// 空数据是否可以滑动
    public func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
        return emptyScroll
    }
    
    /// 空数据背景色
    public func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
        return emptyBgColor == nil ? scrollView.backgroundColor : emptyBgColor
    }
    
    public func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return CGFloat(emptyOffset)
    }
    
}

